/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.shared.dtos;

import com.google.solarchallenge.shared.LoginCategory;

/**
 * DTO for UserAccount.
 *
 * @author Arjun Satyapal
 */
public class UserAccountDto implements AbstractDto {
  private String key;
  private String firstName;
  private String lastName;
  private String email;
  private LoginCategory loginCategory;
  private boolean loginAllowed;

  @Override
  public StringBuilder getStringBuilder() {
    StringBuilder builder = new StringBuilder("ApproverDto[").append("key:")
        .append(key)
        .append(", firstName:")
        .append(firstName)
        .append(", lastName:")
        .append(lastName)
        .append(", email:")
        .append(email)
        .append(", loginCategory:")
        .append(loginCategory)
        .append(", isLoginAllowed:")
        .append(loginAllowed)
        .append("].");

    return builder;
  }

  @Override
  public String toString() {
    return getStringBuilder().toString();
  }

  private UserAccountDto() {
  }

  @Override
  public String validate() {
    StringBuilder builder = new StringBuilder("");

    // id will be set only by server. So not checking it.
    if (firstName.isEmpty()) {
      builder.append("firstName is empty.\n");
    }
    if (lastName.isEmpty()) {
      builder.append("lastName is empty.\n");
    }
    if (email.isEmpty()) {
      builder.append("email is empty.\n");
    }

    return builder.toString();
  }

  public String getKey() {
    return key;
  }

  public void setKey(String key) {
    this.key = key;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public String getEmail() {
    return email;
  }

  public boolean isLoginAllowed() {
    return loginAllowed;
  }

  public LoginCategory getLoginCategory() {
    return loginCategory;
  }

  public static class Builder {
    private String key;
    private String firstName;
    private String lastName;
    private String email;
    private LoginCategory loginCategory;
    private boolean isLoginAllowed;

    public Builder setKey(String key) {
      this.key = key;
      return this;
    }

    public Builder setFirstName(String firstName) {
      this.firstName = firstName;
      return this;
    }

    public Builder setLastName(String lastName) {
      this.lastName = lastName;
      return this;
    }

    public Builder setEmail(String email) {
      this.email = email;
      return this;
    }


    public Builder setIsLoginAllowed(boolean isLoginAllowed) {
      this.isLoginAllowed = isLoginAllowed;
      return this;
    }

    public Builder setLoginCategory(LoginCategory loginCategory) {
      this.loginCategory = loginCategory;
      return this;
    }

    public UserAccountDto build() {
      UserAccountDto dto = new UserAccountDto();
      dto.key = key;
      dto.firstName = firstName;
      dto.lastName = lastName;
      dto.email = email;
      dto.loginCategory = loginCategory;
      dto.loginAllowed = isLoginAllowed;
      dto.validate();
      return dto;
    }
  }
}
